﻿<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>会员登录</title>
    <link rel="stylesheet" href="plugins/elementui/index.css">
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <link rel="stylesheet" href="css/login_style.css">
    <link rel="icon" href="img/favicon.ico">
</head>

<body>
<div id="app">
    <form class="form" id="login-form" autocomplete="off">
        <div class="form-inner">
            <h2 class="logo">会员登录</h2>
            <div class="input-wrapper">
                <label for="login-username">用户名</label>
                <div class="input-group"><span class="icon">
              <svg viewBox="0 0 24 24">
                <path
                        d="M12,4A4,4 0 0,1 16,8A4,4 0 0,1 12,12A4,4 0 0,1 8,8A4,4 0 0,1 12,4M12,14C16.42,14 20,15.79 20,18V20H4V18C4,15.79 7.58,14 12,14Z">
                </path>
              </svg></span>
                    <input type="text" id="login-username" v-model="username" data-lpignore="true">
                </div>
            </div>
            <div class="input-wrapper">
                <label for="login-password">密码</label>
                <div class="input-group"><span class="icon">
              <svg viewBox="0 0 24 24">
                <path
                        d="M11.83,1.73C8.43,1.79 6.23,3.32 6.23,3.32C5.95,3.5 5.88,3.91 6.07,4.19C6.27,4.5 6.66,4.55 6.96,4.34C6.96,4.34 11.27,1.15 17.46,4.38C17.75,4.55 18.14,4.45 18.31,4.15C18.5,3.85 18.37,3.47 18.03,3.28C16.36,2.4 14.78,1.96 13.36,1.8C12.83,1.74 12.32,1.72 11.83,1.73M12.22,4.34C6.26,4.26 3.41,9.05 3.41,9.05C3.22,9.34 3.3,9.72 3.58,9.91C3.87,10.1 4.26,10 4.5,9.68C4.5,9.68 6.92,5.5 12.2,5.59C17.5,5.66 19.82,9.65 19.82,9.65C20,9.94 20.38,10.04 20.68,9.87C21,9.69 21.07,9.31 20.9,9C20.9,9 18.15,4.42 12.22,4.34M11.5,6.82C9.82,6.94 8.21,7.55 7,8.56C4.62,10.53 3.1,14.14 4.77,19C4.88,19.33 5.24,19.5 5.57,19.39C5.89,19.28 6.07,18.92 5.95,18.6V18.6C4.41,14.13 5.78,11.2 7.8,9.5C9.77,7.89 13.25,7.5 15.84,9.1C17.11,9.9 18.1,11.28 18.6,12.64C19.11,14 19.08,15.32 18.67,15.94C18.25,16.59 17.4,16.83 16.65,16.64C15.9,16.45 15.29,15.91 15.26,14.77C15.23,13.06 13.89,12 12.5,11.84C11.16,11.68 9.61,12.4 9.21,14C8.45,16.92 10.36,21.07 14.78,22.45C15.11,22.55 15.46,22.37 15.57,22.04C15.67,21.71 15.5,21.35 15.15,21.25C11.32,20.06 9.87,16.43 10.42,14.29C10.66,13.33 11.5,13 12.38,13.08C13.25,13.18 14,13.7 14,14.79C14.05,16.43 15.12,17.54 16.34,17.85C17.56,18.16 18.97,17.77 19.72,16.62C20.5,15.45 20.37,13.8 19.78,12.21C19.18,10.61 18.07,9.03 16.5,8.04C14.96,7.08 13.19,6.7 11.5,6.82">
                </path>
              </svg></span>
                    <input type="password" name="password" id="login-password" v-model="password" data-lpignore="true">
                </div>
            </div>
            <div class="btn-group">
                <button class="btn btn--primary" type="button" @click="submitForm">登录</button>
                <div class="links">
                    <a class="btn--text" href="register.html">注册账号</a>
                    <span style="margin: 0 10px;">|</span>
                    <a class="btn--text" href="retrieve-password.html">忘记密码？</a>
                </div>
            </div>
        </div>
    </form>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            username: '',
            password: ''
        },
        mounted() {
            // 添加键盘事件监听
            document.addEventListener('keyup', (e) => {
                if (e.key === 'Enter') {
                    this.submitForm();
                }
            });
        },
        beforeDestroy() {
            // 组件销毁前移除事件监听
            document.removeEventListener('keyup', this.handleEnter);
        },
        methods: {
            submitForm() {
                const formData = new FormData();
                formData.append('username', this.username);
                formData.append('password', this.password);

                fetch('/login', {
                    method: 'POST',
                    body: formData
                })
                    .then(data => {
                        if (data.ok) {
                            // 登录成功提示
                            this.$message({
                                message: '登录成功！',
                                type: 'success',
                                duration: 3000
                            });
                            window.location.href = data.url; // 假设成功后跳转到仪表盘
                        } else {
                            data = data.json();
                            data.then(data => {
                                // 登录失败提示
                                this.$message({
                                    message: data.msg || '登录失败。',
                                    type: 'error',
                                    duration: 3000
                                });
                            })

                        }
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        this.$message({
                            message: '请求失败，请稍后重试。',
                            type: 'error',
                            duration: 3000
                        });
                    });
            }
        }
    });
</script>
</body>

</html>